package io.kiki.stack.http.feign.jaxb;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;

/**
 * Provides differents ways to instantiate a JAXB Context.
 */
public enum JAXBContextInstantationMode {
    CLASS {
        @Override
        JAXBContextCacheKey getJAXBContextCacheKey(Class<?> clazz) {
            return new JAXBContextClassCacheKey(clazz);
        }

        @Override
        JAXBContext getJAXBContext(Class<?> clazz) throws JAXBException {
            return JAXBContext.newInstance(clazz);
        }
    },

    PACKAGE {
        @Override
        JAXBContextCacheKey getJAXBContextCacheKey(Class<?> clazz) {
            return new JAXBContextPackageCacheKey(clazz.getPackage().getName(), clazz.getClassLoader());
        }

        @Override
        JAXBContext getJAXBContext(Class<?> clazz) throws JAXBException {
            return JAXBContext.newInstance(clazz.getPackage().getName(), clazz.getClassLoader());
        }
    };

    abstract JAXBContextCacheKey getJAXBContextCacheKey(Class<?> clazz);

    abstract JAXBContext getJAXBContext(Class<?> clazz) throws JAXBException;
}
